package com.security.system.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.security.exception.CustomException;
import com.security.exception.CustomExceptionType;
import com.security.system.dao.SysUserDao;
import com.security.system.entity.SysUser;
import com.security.system.service.SysUserService;
import com.security.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserDao dao;

    @Override
    public R UserTableData(Map<String, Object> param) {
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        List<Map<String, Object>> staffInfos = dao.UserTableData(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(staffInfos);
        return R.table(Integer.parseInt(PageInfos.getTotal()+""),PageInfos.getList());
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = dao.selectByUsername(username);
        if (ObjectUtils.isEmpty(sysUser)){
            log.info("---------------用户名不存在--------------");
            throw new CustomException(CustomExceptionType.USER_INPUT_ERROR,"用户名不存在");
        }
        return sysUser;
    }
}

